package com.yp.infrastructure.axon.interceptor.query;

import com.yp.infrastructure.axon.constants.EnumErrorMessage;
import com.yp.infrastructure.axon.exception.AxonException;
import com.yp.infrastructure.axon.interceptor.BaseMessageHandlerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.messaging.InterceptorChain;
import org.axonframework.messaging.MessageHandlerInterceptor;
import org.axonframework.messaging.unitofwork.UnitOfWork;
import org.axonframework.queryhandling.QueryMessage;

import javax.annotation.Nonnull;

@Slf4j
public class QueryMessageHandlerInterceptor extends BaseMessageHandlerInterceptor
        implements MessageHandlerInterceptor<QueryMessage<?, ?>> {


    @Nonnull
    @Override
    public Object handle(@Nonnull UnitOfWork<? extends QueryMessage<?, ?>> unitOfWork, @Nonnull InterceptorChain interceptorChain) throws Exception {
        try {
            return interceptorChain.proceed();
        } catch (Throwable e) {
            log.error(e.getMessage());
            throw new AxonException(EnumErrorMessage.AXON_ERROR);
        }
    }
}
